import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _regeneratorRuntime from "@babel/runtime/regenerator";

/**
 * 接口字段维护 /ciam/api-field
 * ciamApiField
 * @date 2019-07-10
 */
import { createPagination, getResponse } from 'utils/utils';
import { queryUnifyIdpValue } from 'cpaas-front/lib/services/api';
import { apiFieldApiQuery, apiFieldFieldCreate, apiFieldFieldQuery, apiFieldFieldRemove, apiFieldFieldUpdate } from "../services/apiFieldService";
export default {
  namespace: 'ciamApiField',
  state: {
    requestMethod: [],
    // 请求方式
    fieldType: [],
    // 字段类型
    apiDataSource: [],
    apiPagination: {},
    fieldDataSource: [],
    fieldPagination: {}
  },
  effects: {
    // 初始化 接口查询 值集 等 初始化 数据
    init: /*#__PURE__*/_regeneratorRuntime.mark(function init(_, _ref) {
      var call, put, res, requestMethod;
      return _regeneratorRuntime.wrap(function init$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              call = _ref.call, put = _ref.put;
              _context.next = 3;
              return call(queryUnifyIdpValue, 'CIAM.REQUEST_METHOD');

            case 3:
              res = _context.sent;
              requestMethod = getResponse(res);

              if (!requestMethod) {
                _context.next = 8;
                break;
              }

              _context.next = 8;
              return put({
                type: 'updateState',
                payload: {
                  requestMethod: requestMethod
                }
              });

            case 8:
            case "end":
              return _context.stop();
          }
        }
      }, init);
    }),
    // 初始化 接口查询 值集 等 初始化 数据
    fieldInit: /*#__PURE__*/_regeneratorRuntime.mark(function fieldInit(_, _ref2) {
      var call, put, res, fieldType;
      return _regeneratorRuntime.wrap(function fieldInit$(_context2) {
        while (1) {
          switch (_context2.prev = _context2.next) {
            case 0:
              call = _ref2.call, put = _ref2.put;
              _context2.next = 3;
              return call(queryUnifyIdpValue, 'CIAM.FIELD.TYPE');

            case 3:
              res = _context2.sent;
              fieldType = getResponse(res);

              if (!fieldType) {
                _context2.next = 8;
                break;
              }

              _context2.next = 8;
              return put({
                type: 'updateState',
                payload: {
                  fieldType: fieldType
                }
              });

            case 8:
            case "end":
              return _context2.stop();
          }
        }
      }, fieldInit);
    }),
    // 查询接口
    query: /*#__PURE__*/_regeneratorRuntime.mark(function query(_ref3, _ref4) {
      var payload, call, put, params, res, apis;
      return _regeneratorRuntime.wrap(function query$(_context3) {
        while (1) {
          switch (_context3.prev = _context3.next) {
            case 0:
              payload = _ref3.payload;
              call = _ref4.call, put = _ref4.put;
              params = payload.params;
              _context3.next = 5;
              return call(apiFieldApiQuery, params);

            case 5:
              res = _context3.sent;
              apis = getResponse(res);

              if (!apis) {
                _context3.next = 10;
                break;
              }

              _context3.next = 10;
              return put({
                type: 'updateState',
                payload: {
                  apiDataSource: apis.content,
                  apiPagination: createPagination(apis)
                }
              });

            case 10:
            case "end":
              return _context3.stop();
          }
        }
      }, query);
    }),
    // 查询接口对应字段
    queryFields: /*#__PURE__*/_regeneratorRuntime.mark(function queryFields(_ref5, _ref6) {
      var payload, call, put, permissionId, params, res, fields;
      return _regeneratorRuntime.wrap(function queryFields$(_context4) {
        while (1) {
          switch (_context4.prev = _context4.next) {
            case 0:
              payload = _ref5.payload;
              call = _ref6.call, put = _ref6.put;
              permissionId = payload.permissionId, params = payload.params;
              _context4.next = 5;
              return call(apiFieldFieldQuery, permissionId, params);

            case 5:
              res = _context4.sent;
              fields = getResponse(res);

              if (!fields) {
                _context4.next = 10;
                break;
              }

              _context4.next = 10;
              return put({
                type: 'updateState',
                payload: {
                  fieldDataSource: fields.content,
                  fieldPagination: createPagination(fields)
                }
              });

            case 10:
            case "end":
              return _context4.stop();
          }
        }
      }, queryFields);
    }),
    // 更新字段
    updateField: /*#__PURE__*/_regeneratorRuntime.mark(function updateField(_ref7, _ref8) {
      var payload, call, permissionId, record, res;
      return _regeneratorRuntime.wrap(function updateField$(_context5) {
        while (1) {
          switch (_context5.prev = _context5.next) {
            case 0:
              payload = _ref7.payload;
              call = _ref8.call;
              permissionId = payload.permissionId, record = payload.record;
              _context5.next = 5;
              return call(apiFieldFieldUpdate, permissionId, record);

            case 5:
              res = _context5.sent;
              return _context5.abrupt("return", getResponse(res));

            case 7:
            case "end":
              return _context5.stop();
          }
        }
      }, updateField);
    }),
    // 新增字段
    createField: /*#__PURE__*/_regeneratorRuntime.mark(function createField(_ref9, _ref10) {
      var payload, call, permissionId, record, res;
      return _regeneratorRuntime.wrap(function createField$(_context6) {
        while (1) {
          switch (_context6.prev = _context6.next) {
            case 0:
              payload = _ref9.payload;
              call = _ref10.call;
              permissionId = payload.permissionId, record = payload.record;
              _context6.next = 5;
              return call(apiFieldFieldCreate, permissionId, record);

            case 5:
              res = _context6.sent;
              return _context6.abrupt("return", getResponse(res));

            case 7:
            case "end":
              return _context6.stop();
          }
        }
      }, createField);
    }),
    // 删除字段
    removeField: /*#__PURE__*/_regeneratorRuntime.mark(function removeField(_ref11, _ref12) {
      var payload, call, permissionId, record, res;
      return _regeneratorRuntime.wrap(function removeField$(_context7) {
        while (1) {
          switch (_context7.prev = _context7.next) {
            case 0:
              payload = _ref11.payload;
              call = _ref12.call;
              permissionId = payload.permissionId, record = payload.record;
              _context7.next = 5;
              return call(apiFieldFieldRemove, permissionId, record);

            case 5:
              res = _context7.sent;
              return _context7.abrupt("return", getResponse(res));

            case 7:
            case "end":
              return _context7.stop();
          }
        }
      }, removeField);
    })
  },
  reducers: {
    updateState: function updateState(state, _ref13) {
      var payload = _ref13.payload;
      return _objectSpread(_objectSpread({}, state), payload);
    }
  }
};